<?php

namespace app\home\model;

use app\home\controller\comment;
use Think\Db;
use think\Model;

class commodity extends Model
{
    public function Selling(){
        return Db::table('sg_commodity')->field('name,price,image,details,id')->where(['status'=>3])->limit(6)->select();
    }

    public function Sellings(){
        return Db::table('sg_commodity')->field('name,price,image,id')->where(['status'=>3])->limit(3)->select();
    }

    public function allInfo($cid){
        return Db::table('sg_commodity')->where(['status'=>3,'cid'=>$cid])->select();
    }

    public function coInfo($id){
        return Db::table('sg_commodity')->where(['id'=>$id,'status'=>3])->find();
    }

    public function cartAdd($coid,$num,$uid){
        $a = Db::table('sg_commodity')->field('price')->where('id',$coid)->find();
        $sum = $a['price'] * $num ;
        $data =[
            'coid' => $coid,
            'num' => $num,
            'uid' => $uid,
            'sum' =>$sum
        ];
        return Db::table('sg_cart')->insert($data);
    }

    public function cartInfo($uid,$a = 'c.id,c.coid,co.image,co.name,co.price,c.num,co.details,c.sum'){
        return Db::table('sg_cart')->alias('c')->field($a)->join('sg_commodity co','c.coid = co.id')->where('uid',$uid)->select();
    }

    public function cartDel($id){
        return Db::table('sg_cart')->where('id',$id)->delete();
    }

    public function orderAdd($orInfo,$a,$uid,$gg,$num){
        Db::startTrans();
        try{
            $orid = Db::table('sg_order')->insertGetId($orInfo);
            $a = Db::table('sg_site')->field('name,tel,address,region,code')->where('id',$a)->find();
            $c = [];
            foreach ($gg as $v){
               $c[] =   $v + ['orid'=>$orid];
            }
            $seInfo = [
                'name'=>$a['name'],
                'tel'=>$a['tel'],
                'address'=>$a['address'],
                'region'=>$a['region'],
                'code'=>$a['code'],
                'orid'=>$orid
            ];
            Db::table('sg_order_commodity')->insertAll($c);
            Db::table('sg_order_site')->insert($seInfo);
            Db::table('sg_cart')->where('uid',$uid)->delete();
            Db::commit();
            return 1;
        }catch (\Exception $e){
            Db::rollback();
            return -1;
        }

    }
    public function dfkInfo($a,$uid){
        return Db::table('sg_order')->alias('o')->join('sg_order_commodity oc','o.id = oc.orid')->field('o.number,o.addTime,o.total,oc.coid,oc.name,oc.price,oc.sum,oc.num')->where(['o.status'=>$a,'o.uid'=>$uid])->select();
    }
    public function ddInfo($a,$uid){
        return Db::table('sg_order')->field('number,addTime,total,id')->where(['status'=>$a,'uid'=>$uid])->select();
    }
    public function cartNum($uid){
        return Db::table('sg_cart')->where('uid',$uid)->count();
    }
    public function orderInfo($orid){
        return Db::table('sg_order_commodity')->alias('oc')->join('sg_commodity c','oc.coid = c.id')->field('oc.name,oc.coid,oc.price,oc.sum,oc.num,c.image')->where('orid',$orid)->select();
    }
    public function coInfoOr($coid){
        return Db::table('sg_commodity')->field('name,price')->where('id',$coid)->find();
    }
    public function commentAdd($data,$orid){

        Db::startTrans();
        try {
            Db::table('sg_order')->where('id',$orid)->update(['status'=>5]);
            Db::table('sg_comment')->insertAll($data);
            Db::commit();
            return ['status'=>1,'msg'=>'成功'];
        }catch (\Exception $e){
            Db::rollback();
            return ['status'=>-1,'msg'=>'失败'];
        }
    }
    public function columnInfo(){
        return Db::table('sg_ccolumn')->field('title,id')->select();
    }
}



















